//
//  PhoneLoginViewController.swift
//  SwiftSign
//
//  Created by continue on 2021/5/19.
//

import UIKit

class PhoneLoginViewController: ViewController {

    override var contentHeight: CGFloat {
        return Configs.BaseDimensions.phoneContentHeight.auto()
    }
    
    lazy var basicLoginStackView: StackView = {
        let subviews: [UIView] = [self.phoneTextField,
                                  self.basicCodeStackView,
                                  self.basicLoginButton]
        let view = StackView(arrangedSubviews: subviews)
        view.distribution = .fillEqually
        view.spacing = 10.auto()
        return view
    }()
    
    lazy var basicCodeStackView: StackView = {
        let subviews: [UIView] = [self.codeTextField,
                                  self.codeButton]
        let view = StackView(arrangedSubviews: subviews)
        view.axis = .horizontal
        view.spacing = 10.auto()
        return view
    }()
    
    lazy var phoneTextField: UITextField = {
        let view = UITextField()
        view.borderStyle = .roundedRect
        view.keyboardType = .emailAddress
        view.autocapitalizationType = .none
        view.clearButtonMode = .whileEditing
        return view
    }()

    lazy var codeTextField: UITextField = {
        let view = UITextField()
        view.borderStyle = .roundedRect
        view.autocapitalizationType = .none
        view.clearButtonMode = .whileEditing
        return view
    }()
    
    lazy var basicLoginButton: UIButton = {
        let view = UIButton()
        view.setBackgroundImage(UIImage(named: "icon_button_bg"),
                                for: .normal)
        return view
    }()
    
    lazy var codeButton: UIButton = {
        let view = UIButton()
        view.titleLabel?.font = UIFont.systemFont(ofSize: 12.auto())
        view.titleColorForNormal = UIColor.systemBlue
        view.titleColorForDisabled = UIColor(red: 0/255, green: 131/255, blue: 250/255, alpha: 0.2)
        return view
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    
    override func makeUI() {
        
        codeButton.titleForNormal = "Send"
        basicLoginButton.titleForNormal = "Login"
        phoneTextField.placeholder = "Enter mobile number"
        codeTextField.placeholder = "Enter verification code"
        
        contentView.addSubview(basicLoginStackView)
        basicLoginStackView.snp.makeConstraints { (make) in
            make.left.top.equalTo(30.auto())
            make.right.bottom.equalTo(-30.auto())
        }
        codeButton.snp.makeConstraints { (make) in
            make.height.equalTo(basicCodeStackView)
            make.width.equalTo(80.auto())
        }
    }

    override func bindViewModel() {
        super.bindViewModel()
        guard let viewModel = viewModel as? PhoneLoginViewModel else { return }
        let input = PhoneLoginViewModel.Input(
            username: phoneTextField.rx.text.orEmpty.asDriver(),
            code: codeTextField.rx.text.orEmpty.asDriver(),
            sendTrigger: codeButton.rx.tap.asDriver(),
            basicLoginTrigger: basicLoginButton.rx.tap.asDriver()
        )
        let ouput = viewModel.transform(input: input)
        
        _ = phoneTextField.rx.textInput <-> viewModel.phone
        
        ouput.sendButtonEnabled.drive(codeButton.rx.isEnabled).disposed(by: rx.disposeBag)
        ouput.codeUserInteractionEnabled.drive(codeButton.rx.isUserInteractionEnabled).disposed(by: rx.disposeBag)
        ouput.timer.drive(codeButton.rx.title(for: .normal)).disposed(by: rx.disposeBag)
        ouput.basicLoginEnabled.drive(basicLoginButton.rx.isEnabled).disposed(by: rx.disposeBag)
      
    }
}
